package cn.iocoder.yudao.module.erp.controller.admin.gi.gidn;

import cn.iocoder.yudao.module.erp.controller.admin.gi.gidn.vo.GiDnPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.gi.gidn.vo.GiDnRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.gi.gidn.vo.GiDnSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.gi.GiDnDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.giitem.GiItemDnDO;
import cn.iocoder.yudao.module.erp.service.gi.GiDnService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - ERP物流东宁仓库")
@RestController
@RequestMapping("/erp/gi-dn")
@Validated
public class GiDnController {

    @Resource
    private GiDnService giDnService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP物流东宁仓库")
//    @PreAuthorize("@ss.hasPermission('erp:gi-dn:create')")
    public CommonResult<String> createGiDn(@Valid @RequestBody GiDnSaveReqVO createReqVO) {
        return success(giDnService.createGiDn(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP物流东宁仓库")
//    @PreAuthorize("@ss.hasPermission('erp:gi-dn:update')")
    public CommonResult<Boolean> updateGiDn(@Valid @RequestBody GiDnSaveReqVO updateReqVO) {
        giDnService.updateGiDn(updateReqVO);
        return success(true);
    }

    @PutMapping("/examine")
    @Operation(summary = "ERP物流义乌仓库单据审核")
//    @PreAuthorize("@ss.hasPermission('erp:gi-dn:examine')")
    public CommonResult<Boolean> examineGiDn(@Valid @RequestBody GiDnDO giDnDO) {
        giDnService.examineGiDn(giDnDO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP物流东宁仓库")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('erp:gi-dn:delete')")
    public CommonResult<Boolean> deleteGiDn(@RequestParam("id") String id) {
        giDnService.deleteGiDn(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP物流东宁仓库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('erp:gi-dn:query')")
    public CommonResult<GiDnRespVO> getGiDn(@RequestParam("id") String id) {
        GiDnDO giDn = giDnService.getGiDn(id);
        return success(BeanUtils.toBean(giDn, GiDnRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP物流东宁仓库分页")
//    @PreAuthorize("@ss.hasPermission('erp:gi-dn:query')")
    public CommonResult<PageResult<GiDnRespVO>> getGiDnPage(@Valid GiDnPageReqVO pageReqVO) {
        //现暂不需分页
        pageReqVO.setPageSize(-1);
        PageResult<GiDnDO> pageResult = giDnService.getGiDnPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GiDnRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP物流东宁仓库 Excel")
//    @PreAuthorize("@ss.hasPermission('erp:gi-dn:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportGiDnExcel(@Valid GiDnPageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GiDnDO> list = giDnService.getGiDnPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP物流东宁仓库.xls", "数据", GiDnRespVO.class,
                BeanUtils.toBean(list, GiDnRespVO.class));
    }

    // ==================== 子表（ERP物流东宁仓库货号详情） ====================

    @GetMapping("/gi-item-dn/list-by-no")
    @Operation(summary = "获得ERP物流东宁仓库货号详情列表")
    @Parameter(name = "no", description = "入库编号")
//    @PreAuthorize("@ss.hasPermission('erp:gi-dn:query')")
    public CommonResult<List<GiItemDnDO>> getGiItemDnListByNo(@RequestParam("no") String no) {
        return success(giDnService.getGiItemDnListByNo(no));
    }

}
